package neo4j

{{- $Name := .Name  }}
import (
	"context"
    "reflect"
    "testing"
    "time"
	"{{.Namespace}}/pkg/query-service/domain/{{.aggregate_name}}/view"
    "{{.Namespace}}/pkg/query-service/infrastructure/test"
    "github.com/liuxd6825/dapr-go-ddd-sdk/ddd/ddd_repository"
	"github.com/liuxd6825/dapr-go-ddd-sdk/errors"
	"github.com/liuxd6825/dapr-go-ddd-sdk/utils/randomutils"
)

func Test{{.Name}}ViewRepositoryImpl(t *testing.T) {
	id := randomutils.UUID()
	tenantId := test.TenantId
	ctx := context.Background()

	{{.name}}Repos := New{{.Name}}ViewRepository()
	new{{.Name}} := view.New{{.Name}}View()
	up{{.Name}} := view.New{{.Name}}View()

	t.Run("Create()", func(t *testing.T) {
    {{- range $name, $property := .AllViewProperties}}
        {{- if not $property.IsArray }}
        new{{$Name}}.{{$name}} = {{$property.GoRepositoryTestValue}}
        {{- end }}
    {{- end}}

		if err := {{.name}}Repos.Create(ctx, new{{.Name}}); err != nil {
			t.Error(err)
			return
		}
		if found{{.Name}}, ok, err := {{.name}}Repos.FindById(ctx, new{{.Name}}.TenantId, new{{.Name}}.Id); err != nil {
			t.Error(err)
			return
		} else if !ok {
			t.Error(errors.New("error not found"))
			return
		} else {
			t.Log("found : ", found{{.Name}})
			// 深度对比新建实体与查询实体的属性
            if err := new{{.Name}}.Equal(found{{.Name}}); err!=nil {
                t.Error(err)
            }
		}
	})

	t.Run("Update()", func(t *testing.T) {
        up{{$Name}}.Id = new{{$Name}}.Id
    {{- range $name, $property := .AllViewProperties}}
        {{- if not $property.NameIsId }}
        {{- if not $property.IsArray }}
        up{{$Name}}.{{$name}} = {{$property.GoRepositoryTestValue}}
        {{- end }}
        {{- end }}
    {{- end }}

		if err := {{.name}}Repos.Update(ctx, up{{.Name}}); err != nil {
			t.Error(err)
			return
		}

		if found{{.Name}}, ok, err := {{.name}}Repos.FindById(ctx, up{{.Name}}.TenantId, up{{.Name}}.Id); err != nil {
			t.Error(err)
			return
		} else if !ok {
			t.Error(errors.New("error not found"))
			return
		} else {
			t.Log("found : ", found{{.Name}})
            // 深度对比新建实体与查询实体的属性
            if err := up{{.Name}}.Equal(found{{.Name}}); err!=nil {
                t.Error(err)
            }
		}
	})

	t.Run("FindAll()", func(t *testing.T) {
		if all, ok, err := {{.name}}Repos.FindAll(ctx, new{{.Name}}.TenantId); err != nil {
			t.Error(err)
			return
		} else if ok {
			t.Logf("count = %v", len(all))
		}
	})

	t.Run("FindByIds()", func(t *testing.T) {
		ids := []string{new{{.Name}}.Id, "a832a377-1177-4168-89b9-b8812ea5417b"}
		if idsList, ok, err := {{.name}}Repos.FindByIds(ctx, new{{.Name}}.TenantId, ids); err != nil {
			t.Error(err)
			return
		} else if ok {
			t.Logf(" count = %v", len(idsList))
		}
	})

    t.Run("FindPagingQuery()", func(t *testing.T) {
        query := ddd_repository.NewFindPagingQuery()
        query.SetTenantId(tenantId)
        query.SetSort("{{.AllViewProperties.Names}}")
        query.SetFilter(fmt.Sprintf("id=='%v'", up{{.Name}}.Id))

        if res, isFound, err := {{.name}}Repos.FindPaging(ctx, query); err != nil {
            t.Error(err)
        } else  if isFound {
            t.Log(res.Data)
        } else {
            t.Log("NotFound")
        }
    })

	t.Run("DeleteById()", func(t *testing.T) {
		if err := {{.name}}Repos.DeleteById(ctx, up{{.Name}}.TenantId, up{{.Name}}.Id); err != nil {
			t.Error(err)
			return
		}
	})

}

